$(function () {
	checkLogin();

	var domain_name = (env == 'pre' || env == 'sit') ? 'cnsuning.com' : 'suning.com';
	document.domain = window.domain = domain_name;
	$('#upload1').AjaxFileUpload({
		action: baseurl + '/post/save/fileUpload.do',
		onComplete: uploadCallback
	})
	bindUpload()
	//活动时间
	$('.active-time').on('click', function () {
		$(this).find('i').toggleClass('checked');
		$('.time-box').stop().slideToggle(apDisabled);

	})

	//starMobiscroll配置
	var starMobiscroll = $('#start-time').mobiscroll().date({
		lang: "zh",
		setText: '确定',
		cancelText: '取消',
		dayText: '日',
		monthText: '月',
		yearText: '年',
		dateFormat: 'yy-mm-dd',// 日期格式
		endYear: 2050,
		min: new Date(),
		onCancel: function (event, inst) {
			//取消输入，清空input，并禁用结束时间选择
			$('#start-time,#over-time').val('');
			$('#start-time').attr('placeholder', '起始时间');
			$('#over-time').mobiscroll('option', {
				disabled: true
			});
			$('.admin-post').addClass('disable');
		},
		onSet: function (event, inst) {
			//选定开始时间
			var startDate = $('#start-time').mobiscroll('getVal');
			var overDate = $('#over-time').mobiscroll('getVal');
			if (overDate < startDate) {
				//判断开始时间不得大于结束时间
				$('#over-time').val('');
				$('.admin-post').addClass('disable');
			}
			//修改结束时间参数
			$('#over-time').mobiscroll('option', {
				min: startDate,
				disabled: false
			});
		}
	});
	var overMobiscroll = $('#over-time').mobiscroll().date({
		lang: "zh",
		setText: '确定',
		cancelText: '取消',
		dayText: '日',
		monthText: '月',
		yearText: '年',
		dateFormat: 'yy-mm-dd',// 日期格式
		endYear: 2050,
		disabled: true,
		onCancel: function (event, inst) {
			$('#over-time').val('');
			$('.admin-post').addClass('disable');
		},
		onSet: function (event, inst) {
			apDisabled();
		}
	});

	//增加投票选项
	$('.add-option a').click(function () {
		var $obtionBox = $('.obtion-box');
		var index = $obtionBox.length;
		if (index > 20) {
			alert('选项超过最大数量！');
			return false;
		}
		var zhNum = ["一", "二", "三", "四", "五", "六", "七", "八", "九", '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十'];
		var optionBox = $('<div class="obtion-box" style="display:none"><div class="vote-option"> <input maxlength="20" placeholder="投票选项' + zhNum[index] + '"><span>0/20</span></div><ul class="vote-ul clearfix"><li><a class="addimg" href="javascript:void 0"></a></li></ul></div>');
		$('.add-option').before(optionBox);
		optionBox.slideDown();
		$('.admin-post').addClass('disable');
		bindUpload();
	})
	$('.post-vote').on('input propertychange', '.vote-option input[type="text"]', function () {
		var num = $(this).val().length;
		$(this).siblings('span').html(num + '/20');
		if (num > 20) {
			return false;
		}
	})
	//发布按钮
	function apDisabled() {
		var apDisabled = false;
		$('.post-vote textarea,.post-vote input:visible').each(function () {
			if ($.trim($(this).val()) == '') {
				apDisabled = true;
			}
		})
		if (apDisabled) {
			$('.admin-post').addClass('disable')
		} else (
			$('.admin-post').removeClass('disable')
		)
	}
	$('.post-vote').on('input propertychange', '.post-vote textarea,.post-vote input', function () {
		apDisabled();
	});

	$('.admin-post').click(function () {
		Prepare({
			complete: function (location) {
				var postTitle = $('.vote-title').val();
				var postContent = $('.admin-textarea').val();
				var images = [];
				var itemList = []; var tmp = {}
				tmp.itemContent = postContent;//一期与postContent一样
				tmp.optionList = [];
				$('.vote-option').each(function (i, v) {
					var obj = {};
					obj.images = $(v).siblings('.vote-ul').find('img').map(function (ii, vv) {
						return $(vv).attr('src')
					}).toArray();
					obj.optionName = $(v).find('input').val()
					tmp.optionList.push(obj)

				})
				itemList.push(tmp);
				var startTime = $('#start-time').val();
				var endTime = $('#over-time').val();
				images = $('.uploadfuck').find('img').map(function (i, v) {
					return v.src
				}).toArray().toString();
				$.ajax({
					url: baseurl + '/post/publish/vote.do',
					dataType: 'jsonp',
					data: {
						postTitle: postTitle,
						postContent: postContent,
						images: images,
						poiId: location.poiId,
						startTime: startTime + ' 00:00:00',
						endTime: endTime + ' 00:00:00',
						itemList: JSON.stringify(itemList),

					}
				}).then(function (data) {
					if (data.resultCode == 1) {
						AlertBox({
							type: 'mini',
							msg: data.resultMsg
						})
					}
					else {
						AlertBox({
							type: 'mini',
							msg: '发布投票成功'
						})
						//TODO： jump
					}
				})
			}
		})

	});

})

var uploadel;
var bindUpload = function () {
	//console.log(object);
	if (window.inApp && window.inApp.android) {
		androidUpload2(function (data, target) {
			var ele = $('<li class="uploadfuck"><img></li>');
			ele.find('img').attr('src', data).show();
			if ($(target).hasClass('J-big')) {
				var ul = $(target).siblings('ul');
				ul.append(ele);
				var left = parseInt($(target).css('left'), 10);
				var width = parseInt($(target).css('width'), 10);
				$(target).css('left', left + width + 10)
			}
			else {
				ul = $(target).closest('ul');
				var li = $(target).parent();
				li.before(ele)

			}
			if (ul.find('.uploadfuck').length == 5) {
				$(target).hide();
			}
		})

	} else {
		$('.addimg').off('click').click(function (e) {
			uploadel = e.target;
			$('#upload1').trigger('click');
		})
	}
}

var uploadCallback = function (data) {
	if (!(+data.result)) {

		var ele = $('<li class="uploadfuck"><img></li>');
		ele.find('img').attr('src', data.url).show();
		if ($(uploadel).hasClass('J-big')) {
			var ul = $(uploadel).siblings('ul');
			ul.append(ele);
			var left = parseInt($(uploadel).css('left'), 10);
			var width = parseInt($(uploadel).css('width'), 10);
			$(uploadel).css('left', left + width + 5)
		}
		else {
			ul = $(uploadel).closest('ul');
			var li = $(uploadel).parent();
			li.before(ele)

		}
		if (ul.find('.uploadfuck').length == 5) {
			$(uploadel).hide();
		}
	}
	else {
		AlertBox({
			type: 'mini',
			msg: '上传失败'
		})
	}
}